<?php
declare (strict_types = 1);

namespace app\command;

use GuzzleHttp\Client;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;

class WisemanGoods extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('wisemangoods')
            ->setDescription('the wisemangoods command');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->getGoods();

        // 指令输出
        $output->writeln('wisemangoods');
    }

    protected function getGoods(){
        $wiseman = Db::name('wiseman')->field('id,author_id')->select();
        if($wiseman) {
            $wiseman = $wiseman->toArray();
            $url = 'https://www.doudouxia.com/data-center/douyin/promotion/list';

            $headers = [
                'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
                'Referer' => ' https://servicewechat.com/wx0ba7924743cbcc26/121/page-frame.html',
            ];
            $date = date("Y-m-d",strtotime("-1 day"));
            foreach($wiseman as $k=>$v){
                $author_id = $v['author_id'];
                if($author_id != ''){
                    $data = '{"author_id":"'.$author_id.'","date":"'.$date.'","keyword":"","page":0,"size":20,"selfCategoryId":"","sort":["sales","desc"],"sources":[4,6,8,71,72],"type":"daily"}';
                    $data = json_decode($data, true);
                    $client = new Client();
                    try{
                        $response = $client->request('post', $url, ['json'=>$data, 'timeout'=>5, 'headers'=>$headers]);
                        $result = json_decode($response->getBody()->getContents(), true);
                        //echo '<pre>';print_r($result);exit;
                        if(isset($result['result'])){
                            if($result['result']['content']){
                                foreach($result['result']['content'] as $author){
                                    $d = [
                                        'title' => $author['title'],
                                        'cover' => $author['promotionId']['cover'],
                                        'e_shop_name' => $author['e_shop_name'],
                                        'e_shop_url' => $author['e_shop_url'],
                                        'product_id' => $author['product_id'],
                                        'sales' => $author['sales'],
                                        're30_sales' => $author['re30_sales'],
                                        'view' => $author['views'],
                                        're30_views' => $author['re30_views'],
                                        'price' => $author['price'],
                                        'author_id' => $author_id,
                                        'date' => $author['date'],
                                        'create_time' => time(),
                                    ];

                                    $res = Db::name('wiseman_goods')->where(['author_id'=>$author_id, 'product_id'=>$author['product_id']])->find();
                                    if(!$res){
                                        Db::name('wiseman_goods')->insert($d);
                                    }else{
                                        Db::name('wiseman_goods')->where(['author_id'=>$author_id, 'product_id'=>$author['product_id']])->update($d);
                                    }
                                }
                            }
                        }

                    }catch (\Exception $e){
                        Log::info('调用接口失败' . $e->getMessage());
                        continue;
                    }

                }

                echo '.';
                if($k % 100 == 0) sleep(1);
            }

        }
    }
}
